capture log close
cd "D:\OneDrive - Syddansk Universitet\Forskning\PhD undervisning, kurser, hjemmeside\PhD sporgeskemakursus\02. Udvidet kursus\Stata commands and Excel"
* log using "exercise_reproducibility, 2 raters.txt", text replace 
use "data, 2 raters.dta", clear
set more off

******************
* Preparing data *
******************

rename patient idnr
rename anders score1
rename lars score2

*****************************
* Pearson correlation coef. *
*****************************

pwcorr score1 score2, sig

*****************
* Paired t-test *
*****************

ttest score1 = score2

reshape long score, i(idnr) j(rater)

label var rater "Anders og Lars"
label var score "Score, Nm"
label var idnr "ID nummer"
label def rater 1 "Anders" 2 "Lars"
label val rater rater

***************************************
* Calculating ICC[2.1A] and ICC[2.1C] *
***************************************

* (1) using icc command

icc score idnr rater, consistency
icc score idnr rater, absolute

* (2) using repeated measures ANOVA

xtmixed score || _all: R.idnr || _all: R.rater, reml variance

* Extracting numbers needed to calculate variance components

matrix b = e(b)
matrix list b

scalar idnr_var_col = colnumb(b, "lns1_1_1:_cons")
scalar id_variance = exp(b[1, idnr_var_col])^2

scalar rater_var_col = colnumb(b, "lns1_2_1:_cons")
scalar rater_variance = exp(b[1, rater_var_col])^2

scalar res_var_col = colnumb(b, "lnsig_e:_cons")
scalar res_variance = exp(b[1, res_var_col])^2

display id_variance, rater_variance, res_variance

scalar icc2_1_c = id_variance/(id_variance + res_variance)
di "ICC 2.1 consistency: " %9.3f icc2_1_c

scalar icc2_1_a = id_variance/(id_variance + rater_variance + res_variance)
di "ICC 2.1 agreement: " %9.3f icc2_1_a

*******************************************
* SEM and SDC (consistency and agreement) *
*******************************************

*-----------------*
* SEM consistency *
*-----------------*

* (1) SEM consistency = sqrt(residual variance)
scalar sem_c = sqrt(res_variance)
di "SEM_consistency: " %9.2f sem_c

* (2) SEM consistency = SD * sqrt(1-ICC_consistency)
sum score
scalar sd_score = r(sd)

di "SEM_consistency: " %9.2f sd_score * sqrt(1-icc2_1_c)

*-----------------*
* SDC consistency *
*-----------------*

* (1) SDC consistency = 1.96 * sqrt(2) * SEM_consistency
di "SDC_consistency: " %9.2f 1.96 * sqrt(2) * sem_c

* (2) SDC consistency = 1*96 * SD_change (from B&A LOA plot)

di "SDC_consistency: " %9.2f 1.96 * 2.971

*---------------*
* SEM agreement *
*---------------*

* (1) SEM agreement = sqrt(systematic variance + residual variance)
scalar sem_a = sqrt(rater_variance + res_variance)
di "SEM_agreement: " %9.2f sem_a

* (2) SEM agreement = SD * sqrt(1-ICC_agreement)
di "SEM_agreement: " %9.2f sd_score * sqrt(1-icc2_1_a)

*---------------*
* SDC agreement *
*---------------*

* (1) SDC agreement = 1.96 * sqrt(2) * SEM_agreement
di "SDC-agreement: " %9.2f 1.92 * sqrt(2) * sem_a

clear
log close
